--
-- Author: mm
-- Date: 2018-03-20 18:07:32
--
-- NewActivityDungeonsController
--

local NewActivityDungeonsView = import("..views.NewActivityDungeonsView")

local NewActivityDungeonsSuccessView = import("..views.NewActivityDungeonsSuccessView")
local NewActivityDungeonsFailView = import("..views.NewActivityDungeonsFailView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("NewActivityDungeonsModel")
    self._pvpModel = app:getInst("PvpModel")

    self._walletHandler = UD:onChanged("wallet", handler(self, self.updatePlayer))
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)
    UD:unChanged("wallet", self._walletHandler)
end

function ClassRef:listEvent()
    return {
        "onDailyUpdate",
        "onMapChanged",
    }
end

function ClassRef:onEvent(eventName, data)
    if eventName == "onDailyUpdate" then
        self._model:C2S_WorldEventInfo()
    elseif eventName == "onMapChanged" then
        if data then
            self._model:C2S_WorldEventInfo()
        end
    end
end

function ClassRef:openView(data)
    if cc.isDead(self._view) then
        self._view = NewActivityDungeonsView.new( app.mvc.VIEWTYPE_BLUR )
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
    end
    self:addChild(self._view)

    local TeamModel = app:getInst("TeamModel")
    local targets = TeamModel:getActivityTeamTargets()
    self._view:refreshTargets(targets)
    self._view:show()

    -- 请求
    self._pvpModel:reqChallengeInfo()
    self:updateChallengeInfo()
    self:updateChallengeStatus()
end

-------------------------------------------------------
-- 活动副本胜利UI
function ClassRef:openSuccessView(data)
    if nil == self._successView then
        self._successView = NewActivityDungeonsSuccessView.new()
        self._successView:onCloseView(handler(self, self.closeSuccessView))
        self._successView:onClearView(handler(self, self.clearSuccessView))
        self._successView:onOperate(handler(self, self.onOperate))
        self:addChild(self._successView)
    end
    self._successView:setVisible(false)
    local openSuccessViewFunc = function()
        self._successView:show()
        self._successView:refreshContent( data )
    end
    if Enums.TeamTarget.lionHeart == tonumber( data.id ) then -- 如果收到的是狮心号角的结算通知
        local delayTime = 0
        local isShown = app:sendMsg( "LionHornController", "isRoundEndViewShown" )
        if isShown then
            delayTime = 3
        end
        self._successView:performWithDelay( function()
            openSuccessViewFunc()
        end, delayTime )
    elseif Enums.TeamTarget.chess == tonumber( data.id ) then
        self._successView:performWithDelay( function()
            openSuccessViewFunc()
        end, 2 )
    else
        openSuccessViewFunc()
    end
end
function ClassRef:closeSuccessView(data)
    if self._successView then
        self:removeChild(self._successView, data)
    end
end
function ClassRef:clearSuccessView()
    if self._successView then
        self._successView = nil
    end
end

-------------------------------------------------------
-- 活动副本失败UI
function ClassRef:openFailView(data)
    if nil == self._failView then
        self._failView = NewActivityDungeonsFailView.new()
        self._failView:onCloseView(handler(self, self.closeFailView))
        self._failView:onClearView(handler(self, self.clearFailView))
        self._failView:onOperate(handler(self, self.onOperate))
        self:addChild(self._failView)
    end
    self._failView:setVisible(false)
    local openFailViewFunc = function()
        self._failView:show()
        self._failView:refreshContent( data )
    end
    local dataId = tonumber( data.id )
    if Enums.TeamTarget.lionHeart == dataId then -- 如果收到的是狮心号角的结算通知
        local delayTime = 0
        local isShown = app:sendMsg( "LionHornController", "isRoundEndViewShown" )
        if isShown then
            delayTime = 3
        end
        self._failView:performWithDelay( function()
            openFailViewFunc()
        end, delayTime )
    elseif Enums.TeamTarget.chess == dataId then
        if not data.score then
            openFailViewFunc()
        end
    else
        openFailViewFunc()
    end
end
function ClassRef:closeFailView(data)
    if self._failView then
        self:removeChild(self._failView, data)
        self:clearFailView()
    end
end
function ClassRef:clearFailView()
    if self._failView then
        self._failView = nil
    end
end

---------------------------------------------------
-- 挑战赛赛季信息
function ClassRef:updateChallengeInfo(info)
    if not self:isViewAlive() then return end

    info = info or self._pvpModel:getChallengeInfo()
    if info then
        self._view:refreshChallengeInfo(info)
    end
end

-- 挑战赛状态
function ClassRef:updateChallengeStatus(status)
    if not self:isViewAlive() then return end

    status = status or self._pvpModel:getChallengeStatusData()
    if status then
        self._view:refreshChallengeStatus(status)
    end
end

-- 监测活动点数变化
function ClassRef:updatePlayer(name, old, new)
    if name == "eventPoint" or name == "eventLuckBox" then
        if self:isViewAlive() then
            self._view:refreshEventBoxInfo()
        end
    end
end
-------------------------------------------------------
function ClassRef:onOperate(op, data)
    print("NewActivityDungeonsController:onOperate op = " .. op)
    if op == "activityDungeons_getReward" then -- 活动副本领取宝箱
        app:sendMsg("BoxController", "openView", { boxType = Enums.BoxType.eventBox })
    elseif op == "activityDungeons_openFunc" then
        -- data = funcId
        UIHandler:handleMessage({id=data, mode="open"})
    elseif op == "close_NewActivityDungeonsResultView" then -- 关闭结算UI、传送场景
        if Enums.TeamTarget.ashInvade ~= tonumber( data.id ) then -- 非灰烬入侵？
            app.session:request("pb_map.C2S_BackToLastMap", {})
        end
    end
end

return ClassRef
